/* This file computes values for uncontested lagged vote. 
   Note: Loop over elections needs to match loop in MAIN.PRG
   Jason Wittenberg, 12/20/2001
*/

new;
library cml,lpack,comp2,probs;
#include loaduk.prg;
NC=.001;
origpca=pca;                  /* Save district ID for original dataset */

rndseed 123456789;            /* Fix seed */

year = {};

unimpy = {};
distid = {};

for i (2,11,1);            /* Remember to change this back to 2,11
                               for all elecs */
                               
  /** Set up data for this run **/
  con1  = varget(vrs[i-1,1]);
  lab1  = varget(vrs[i-1,2]);
  lib1  = varget(vrs[i-1,3]);
  con2  = varget(vrs[i,1]);
  lab2  = varget(vrs[i,2]);
  lib2  = varget(vrs[i,3]);
  ninc2 = varget(vrs[i,5]);
  pca=origpca;
  
  dataset = packr(con1~lab1~lib1~con2~lab2~lib2~ninc2~pca);
  N = rows(dataset);
  V1 = dataset[.,1:3];
  V2 = dataset[.,4:6];
  pca = dataset[.,8];
  
  /* Create indicator to transform lagged vote */
  
  MP1 = -9*ones(rows(V1[.,1]),1);
  yr= years[i]*ones(rows(V1[.,1]),1);
  MP1 = substute(MP1,V1[.,1].<=NC .and V1[.,2].>NC  .and V1[.,3].>NC,1);
  MP1 = substute(MP1,V1[.,1].>NC  .and V1[.,2].<=NC .and V1[.,3].>NC,2);
  MP1 = substute(MP1,V1[.,1].>NC  .and V1[.,2].>NC  .and V1[.,3].<=NC,3);
  MP1 = substute(MP1,V1[.,1].>NC  .and V1[.,2].<=NC .and V1[.,3].<=NC,4);
  MP1 = substute(MP1,V1[.,1].<=NC .and V1[.,2].>NC  .and V1[.,3].<=NC,5);
  MP1 = substute(MP1,V1[.,1].<=NC .and V1[.,2].<=NC .and V1[.,3].>NC,6);
  
  
  /* Want to save Y1 without the imputation */
  /* Problem: Leaving the zeros in means the ln will
     explode! */
  /* COMP2.SRC provides a solution. It
     transforms the vote (not the lagged vote)
     onto the logistic scale. Vote is the DEPVAR. */
     
  /** Convert lagged yote to logit scale, w/o imputation **/
  
  v1temp=V1[.,3];
  V1temp = substute(V1temp,V1temp.<=NC,0.001);
  
  v1_1temp=v1[.,1];
  v1_1temp = substute(v1_1temp,v1_1temp.<=NC,0.001);
  v1_2temp=v1[.,2];
  v1_2temp = substute(v1_2temp,v1_2temp.<=NC,0.001);
  
  y11=ln(v1_1temp./v1temp);
  y12=ln(v1_2temp./v1temp);
  
  y1=y11~y12;
  
  /* Collect useful data */
  year = year | yr;
  
  distid = distid | pca;
  
  /* Now need to deal with dependent variable */
  
  vv1=v2[.,1];    /* CON2 */
  vv2=v2[.,2];    /* LAB2 */
  vv3=v2[.,3];    /* LIB2 */
  
  
  /* The following code is adapted from COMP2.SRC */
  
  MP = -9*ones(rows(vv1),1);
  MP = substute(MP,vv1.>NC .and vv2.>NC .and vv3.>NC,0);
  MP = substute(MP,vv1.<=NC .and vv2.>NC .and vv3.>NC,1);
  MP = substute(MP,vv1.>NC .and vv2.<=NC .and vv3.>NC,2);
  MP = substute(MP,vv1.>NC .and vv2.>NC .and vv3.<=NC,3);
  MP = substute(MP,vv1.>NC .and vv2.<=NC .and vv3.<=NC,4);
  MP = substute(MP,vv1.<=NC .and vv2.>NC .and vv3.<=NC,5);
  MP = substute(MP,vv1.<=NC .and vv2.<=NC .and vv3.>NC,6);
  
  vv3a = substute(vv3,vv3.<=NC,0.001);
  yy1 = ln(vv1./vv3a);
  ex1 = .NOT (MP.==0 .or MP.==2);
  yy1 = missex(yy1,ex1);

  yy2 = ln(vv2./vv3a);
  ex2 = .NOT (MP.==0 .or MP.==1);
  yy2 = missex(yy2,ex2);

  vv2a = substute(vv2,vv2.<=NC,0.001);
  yy3 = ln(vv1./vv2a);
  yy3 = missex(yy3,MP./=3);
  
  yy = yy1~yy2~yy3;
  
  unimpy = unimpy | yy;   /* Unimpy--3 cols */
   
endfor;

output file=ukout2.out reset;
distid~year~unimpy[.,1]~unimpy[.,2];
output off;

"Please run the Stata do file mkukdta.do";


  
